package br.inatel.ec203.projeto.view;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameEvent;

import br.inatel.ec203.projeto.model.Capacitor;
import br.inatel.ec203.projeto.model.Ci;
import br.inatel.ec203.projeto.model.Item;
import br.inatel.ec203.projeto.model.OutrosComponentes;
import br.inatel.ec203.projeto.model.Resistor;
import br.inatel.ec203.projeto.model.Transistor;

public class SearchFrame extends JFrame {

	private static final long serialVersionUID = 3697243264676663077L;
	
	private SearchPanel searchPanel;
	private ViewDebitCancelButtonsPanel viewDebitCancelButtonsPanel;

	public SearchFrame() {
		super ("Tela para pesquisa");	
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		initialize();
		setSize(new Dimension(500, 600));
		setResizable(false);
		setLocationRelativeTo(null);
	}

	private void initialize() {
		add(getSearchPanel(), BorderLayout.PAGE_START);
	}
	
	public SearchPanel getSearchPanel() {
		if (searchPanel == null) {
			searchPanel = new SearchPanel();
			searchPanel.addSearchPanelListeners(new SearchPanelListeners() {
				
				@Override
				public void componentRadioButtonPerformed() {
					searchPanel.remove(searchPanel.getPartNumberSearchPanel());
					searchPanel.remove(searchPanel.getComponentScrollPane());
					remove(getViewDebitCancelButtonsPanel());
					
					searchPanel.add(searchPanel.getValueSearchPanel(),searchPanel.getAmountSearchPanelConstraints());
					searchPanel.add(searchPanel.getComponentScrollPane(), searchPanel.getComponentScrollPaneConstraints());
					searchPanel.getValueSearchPanel().getValueTextField().setText("");
					add(getViewDebitCancelButtonsPanel(), BorderLayout.PAGE_END);
					
					searchPanel.fillTable();
					repaint();
					setVisible(isDisplayable());
				}

				@Override
				public void partNumberRadioButtonPerformed() {
					searchPanel.remove(searchPanel.getValueSearchPanel());
					searchPanel.remove(searchPanel.getComponentScrollPane());
					remove(getViewDebitCancelButtonsPanel());
					
					searchPanel.add(searchPanel.getPartNumberSearchPanel(), searchPanel.getPartNumberSearchPanelConstraints());
					searchPanel.add(searchPanel.getComponentScrollPane(), searchPanel.getComponentScrollPaneConstraints());
					searchPanel.getPartNumberSearchPanel().getPartNumberTextField().setText("");
					add(getViewDebitCancelButtonsPanel(), BorderLayout.PAGE_END);
					
					searchPanel.fillTable();
					repaint();
					setVisible(isDisplayable());
				}

				@Override
				public void searchButtonOfAmountPerformed() {
					searchPanel.searchByComponent();
				}

				@Override
				public void searchButtonOfPartNumberPerformed() {
					searchPanel.searchByPartNumber();
				}
			});
		}
		return searchPanel;
	}
	
	public ViewDebitCancelButtonsPanel getViewDebitCancelButtonsPanel() {
		if (viewDebitCancelButtonsPanel == null) {
			viewDebitCancelButtonsPanel = new ViewDebitCancelButtonsPanel();
			viewDebitCancelButtonsPanel.addViewDebitCancelButtonsListeners(new ViewDebitCancelButtonsListeners() {
				
				@Override
				public void viewPerformed() {
					viewClicked();
				}
				
				@Override
				public void debitPerformed() {
					debitClicked();
				}
				
				@Override
				public void cancelPerformed() {
					dispose();
				}
			});
		}
		return viewDebitCancelButtonsPanel;
	}

	protected void debitClicked() {
		try {
			String value = (String) getSearchPanel().getComponentTable().getValueAt(getSearchPanel().getComponentTable().getSelectedRow(), 1);
			String partNumber = (String) getSearchPanel().getComponentTable().getValueAt(getSearchPanel().getComponentTable().getSelectedRow(), 2);
			
			Item component = Util.findComponentInArrayList(value, partNumber);
			if (component != null) {
				DebitFrame debitFrame = new DebitFrame(component);
				debitFrame.setVisible(true);
				debitFrame.addDebitFrameListeners(new DebitFrameListeners() {
					@Override
					public void internalFrameClosing(InternalFrameEvent e) {
						getSearchPanel().fillTableBySearch();
					}
				});
							
			} else {
				JOptionPane.showMessageDialog(null, "Componente nao encontrado", "Erro", JOptionPane.ERROR_MESSAGE);
			}
		}catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Selecionar Componente", "Erro", JOptionPane.ERROR_MESSAGE);
		}
	}

	protected void viewClicked() {
		try {
			int rowNum = getSearchPanel().getComponentTable().getSelectedRow();
			String value = (String) getSearchPanel().getComponentTable().getValueAt(rowNum, 1);
			String partNumber = (String) getSearchPanel().getComponentTable().getValueAt(rowNum, 2);
			Item componente = Util.findComponentInArrayList(value, partNumber);
			if (componente instanceof Resistor) {
				viewComponent(componente);			
			}
			else if (componente instanceof Capacitor) {
				viewComponent(componente);
			}
			else if (componente instanceof Ci) {
				viewComponent(componente);
			}
			else if (componente instanceof Transistor) {
				viewComponent(componente);
			}
			else if (componente instanceof OutrosComponentes) {
				viewComponent(componente);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Selecionar Componente", "Erro", JOptionPane.ERROR_MESSAGE);
		}

	}

	private void viewComponent(Item componente) {
		JOptionPane.showMessageDialog(null, componente.toStringToPrint(), "Visualizacao de componente", JOptionPane.DEFAULT_OPTION);
	}
		
}
















